import GlobalParamsController from '../../core/controllers/GlobalParams.controller';

@Inject('$log', '$scope', '$state', 'trainingService', 'ngDialogService', 'uiUploader')
class educationTrainingRegisterController extends GlobalParamsController {
  constructor() {
    super();

    this.view = {
      joinDepartmentObj: [],
      files: [],
    };

    this.data = {
      isEdit: false,
      activity: {
        name: '',
        startTime: '',
        organizer: '',
        site: '',
        speaker: '',
        recorder: '',
        content: '',
        technicalType: '',
        joinDepartment: '',
        attachmentIds: null,
      },
    };
  }

  submitActivityForm() {
    const deptCodes = this.view.joinDepartmentObj.map(item => item.code);
    const attachmentId = this.view.files.map(item => item.id);
    this.data.activity.joinDepartment = deptCodes.join(',');
    this.data.activity.attachmentIds = attachmentId;
    this.trainingService.save(this.data.activity)
      .then((res) => {
        if (res.success) {
          this.ngDialogService.alertSuccess(res.message);
          this.$state.go('toDoSafeActivities');
          // window.history.go(-1);  // go(-1) 有可能回退到百度,web不是手机App,不能这样回退的
        } else {
          this.ngDialogService.alertError(res.message);
        }
      });
  }
}

export default educationTrainingRegisterController;
